/*
 * Stephen Herbein, Ronald Lewis, and Kyle Tucker, CISC220080, Dr. Atlas
 * 
 * Outputs a text file containing traffic data obtained from an image file.
 */

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class CISC220Project {

	public static void main(String[] args){
		
		// Holds the image being analyzed
		BufferedImage image = null;
		// Opens the traffic image file
		try{
			ImageIcon icon = new ImageIcon("flow.gif");
		    Image image2 = icon.getImage();
		    image = new BufferedImage(image2.getWidth(null), image2.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		    Graphics g = image.getGraphics();
		    g.drawImage(image2, 0, 0, null);
		}
		// Print any Exceptions caught
		catch(Exception e){
			System.out.println(e);
		}
		// Holds all the yellow and red spots found in the image
		ArrayList<ColorPoint> colors = new ArrayList<ColorPoint>();
		// Holds the color of the current pixel
		Color current = null;
		// Holds the red component of the current pixel
		int red = 0;
		// Holds the green component of the current pixel
		int green = 0;
		// Holds the blue component of the current pixel
		int blue = 0;
		// Moves through the y values of the image
		for (int j = 0; j < image.getHeight(null); j += 1){
			// Moves through the x values of the image
			for (int i = 0; i < image.getWidth(null); i += 1){
				// Gets the color of the current pixel
				current = new Color(image.getRGB(i,j));
				// Gets the red component of the current pixel
				red = current.getRed();
				// Gets the green component of the current pixel
				green = current.getGreen();
				// Gets the blue component of the current pixel
				blue = current.getBlue();
				// Triggers if the current pixel is red
				if((red > 150) && (green < 100) && (blue < 100)){
					// Adds the current pixel to colors
					colors.add(new ColorPoint("R",i,j));
				}
				// Triggers if the current pixel is yellow
				else if((green > 200) && (red > 200) && (blue < 10)){
					// Adds the current pixel to colors
					colors.add(new ColorPoint("Y",i,j));
				}
			}
		}
		// Holds the output to be written to a text file
		String output = "";
		// Moves through the elements of colors
		for(ColorPoint c: colors){
			// Adds the element to the output ensuring the x and y values
			// have 3 digits
			output += c.color + ",";
			if((c.x / 10) == 0){
				output += "00";
			}
			else if((c.x / 100) == 0){
				output += "0";
			}
			output += Integer.toString(c.x) + ",";
			if((c.y / 10) == 0){
				output += "00";
			}
			else if((c.y / 100) == 0){
				output += "0";
			}
			output += Integer.toString(c.y) + ";";
		}
		// Tries to write the output to a text file
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter("trafficData.txt"));
			writer.write(output);
			writer.close();
		}
		// Prints any Exceptions caught
		catch(Exception e){
			System.out.println(e);
		}
	}
	
}

class ColorPoint{
	
	/*
	 * Holds the output information for a yellow or red pixel.
	 */
	
	String color;
	int x;
	int y;
	
	public ColorPoint(String color, int x, int y){
		
		/*
		 * Constructor for the class.
		 */
		
		this.color = color;
		this.x = x;
		this.y = y;
		
	}
	
	public String toString(){
		
		/*
		 * Returns the string representation of an instance of the class.
		 */
		
		return color + "," + Integer.toString(x) + "," + Integer.toString(y);
		
	}
	
}
